package com.open.harmony.business.service.serviceImpl;

import com.open.harmony.business.entity.OrderItem;
import com.open.harmony.business.mapper.OrderItemMapper;
import com.open.harmony.business.service.OrderItemService;
import com.open.harmony.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class OrderItemServiceImpl implements OrderItemService {

    private final OrderItemMapper orderItemMapper;

    @Autowired
    public OrderItemServiceImpl(OrderItemMapper orderItemMapper) {
        this.orderItemMapper = orderItemMapper;
    }

    @Override
    public Result findAll(Integer userId) {
        List<OrderItem> orderItems = orderItemMapper.findAll(userId);
        return Result.success(orderItems);
    }
    @Override
    public Result delete(Integer userId, Integer productId) {
        Integer res = orderItemMapper.delete(userId, productId);
        if(res == 0){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    /**
     *  更改商品购买数量，即购物车物品数量
     * @param userId 用户id (用户就是商家)
     * @param productId 购物车商品id
     * @param num 要更改商品数量
     * @return 更改结果
     */
    @Override
    public Result updateNum(Integer userId, Integer productId, Double num) {
        if(num == 0){
            return delete(userId, productId);
        }

        Integer res = orderItemMapper.updateNum(userId, productId, num);
        if(Objects.isNull(res) || res == 0){
            return Result.error("更改失败");
        }
        return Result.success("更改数量成功");
    }


    @Override
    public Result deleteAll(Integer userId) {
        Integer res = orderItemMapper.deleteAll(userId);
        if(res == 0){
            return Result.error("清除全部失败");
        }
        return Result.success("清除全部成功");
    }

    /**
     *  没有就添加，有者更改
     * @param userId
     * @param productId
     * @param num 要更改的数量
     * @return 添加或更新结果
     */
    @Override
    public Result addItem(Integer userId, Integer productId, Double num) {
        if(Objects.isNull(userId) || Objects.isNull(productId) || Objects.isNull(num)){
            return Result.error("参数为空");
        }
        Integer cnt = orderItemMapper.findByProduct(userId, productId);
        if(!Objects.isNull(cnt)){
            return updateNum(userId, productId, num);
        }
        Integer res = orderItemMapper.addItem(userId, productId, num);
        if(res == 0){
            return Result.error("添加失败");
        }
        return Result.success("添加成功");
    }

    @Override
    public Result payment(Integer userId) {
        Double shopTotalPrice = orderItemMapper.getShopTotalPrice(userId);
        if(Objects.isNull(shopTotalPrice) || shopTotalPrice == 0){
            return Result.error("购物车为空");
        }
        Integer createOrder = orderItemMapper.insertOrder(userId, shopTotalPrice);
        if(Objects.isNull(createOrder) || createOrder == 0){
            return Result.error("创建订单失败");
        }
        Integer updateItem = orderItemMapper.updateOrderItem(userId);
        if(Objects.isNull(updateItem) || updateItem == 0){
            return Result.error("将购物车和订单绑定失败");
        }
        return Result.error("生成订单成功");
    }
}
